import * as Cesium from 'cesium';
import gsap from 'gsap';

export default class LightSpreadMaterialProperty {
  constructor(name) {
    this.name = name;

    this.definitionChanged = new Cesium.Event();
    Cesium.Material._materialCache.addMaterial('LightSpreadMaterial', {
      fabric: {
        type: 'LightSpreadMaterial',
        uniforms: {
          uTime: 0,
          image: './texture/hexagon.png',
        },
        source: `
          // 声明全局的纹理采样器
          uniform sampler2D image; 
          czm_material czm_getMaterial(czm_materialInput materialInput) {
            // 获取默认材质
            czm_material material = czm_getDefaultMaterial(materialInput);

            vec2 st = materialInput.st;
            //根据uv采样颜色
            vec4 color = texture(image, st);
            material.diffuse = color.rgb;
            material.alpha = color.a;

            return material;
          }
        `,
      },
    });

    this.params = {
      uTime: 0,
      image: './texture/hexagon.png',
    };
    gsap.to(this.params, {
      uTime: 2 * 3.14159, // 让 uTime 覆盖一个完整的圆周
      duration: 4,
      repeat: -1,
      //线性
      ease: 'linear',
      //来回
      // yoyo: true,
    });
  }
  getType() {
    // 返回材质类型
    return 'LightSpreadMaterial';
  }
  getValue(time, result) {
    result.uTime = this.params.uTime;
    result.image = this.params.image; // 传递 image 这个 uniform
    return result;
  }
  equals(other) {
    // 判断两个材质是否相等
    return (
      other instanceof LightSpreadMaterialProperty && this.name === other.name
    );
  }
}
